package com.briup.recognition.web.controller;

import com.briup.recognition.bean.GreenhouseMessage;
import com.briup.recognition.service.IMessageService;
import com.briup.recognition.utils.Message;
import com.briup.recognition.utils.MessageUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private IMessageService iMessageService;
    @GetMapping("query")
    public Message query(){
        List<GreenhouseMessage> greenhouseMessages = iMessageService.query();
                return MessageUtil.success(
                        greenhouseMessages
                );
    }

    @PostMapping("saveOrUpdte")
    public Message saveOrUpdte(GreenhouseMessage message){
        Integer msgnum=iMessageService.saveOrUpdate(message);
        if (message==null){
            return MessageUtil.error(
                    50000,
                    "未输入信息，请输入信息！"
            );
        }else {
            if (message.getId()==null && msgnum == 1){

                return MessageUtil.success(
                        "插入数据成功"
                );
            }else if (message.getId()!=null && msgnum == 2){
                iMessageService.saveOrUpdate(message);
                return MessageUtil.success(
                        "更新数据成功"
                );
            }
            return MessageUtil.error(
                    50000,
                    "输入信息有误！"
            );

        }
    }
    @DeleteMapping("deleteById")
    public Message deleteById(Long id){

            iMessageService.deleteById(id);
            return MessageUtil.success(
              "删除成功！"
            );
    }



}
